package codetop.microsoft.T224;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author: 18362
 * @Create: 2022-08-19 9:30:48 星期五
 */
class Solution {
    private int cur;
    public int calculate(String s) {
        return getValue(s);
    }

    private int getValue(String s) {
        int value = 0;
        char opt = ' ';
        while (cur < s.length()) {
            char c = s.charAt(cur);
            if (c == ')') {
                cur++;
                return value;
            } else if (c == '(') {
                cur++;
                int v = getValue(s);
                if (opt == '-')
                    value -= v;
                else
                    value += v;
            } else if (c >= '0' && c <= '9') {
                int num = getNum(s);
                if (opt == '-')
                    value -= num;
                else
                    value += num;
            } else if (c == '+' || c == '-') {
                opt = c;
                cur++;
            } else {
                cur++;
            }
        }
        return value;
    }

    private int getNum(String s) {
        int res = 0;
        while (cur < s.length()) {
            char c = s.charAt(cur);
            if (c >= '0' && c <= '9')
                res = res * 10 + c - '0';
            else
                break;
            cur++;
        }
        return res;
    }
}